﻿/*
 * YangH265EncoderSoft.h
 *
 *  Created on: 2020年10月14日
 *      Author: yang
 */

#ifndef YANGENCODER_SRC_YANGH265ENCODERSOFT_H_
#define YANGENCODER_SRC_YANGH265ENCODERSOFT_H_


#include "yangencoder/YangVideoEncoder.h"
#include "stdint.h"
//extern "C"{
#include "x265.h"
//}
#include "yangutil/buffer/YangVideoBuffer.h"
#include "yangutil/buffer/YangVideoEncoderBuffer.h"
#include "yangutil/sys/YangIni.h"
#include "yangutil/sys/YangLoadLib.h"


class YangH265EncoderSoft : public YangVideoEncoder {
public:
	YangH265EncoderSoft();
	virtual ~YangH265EncoderSoft();
	static void initX265Param(YangVideoInfo *pvp,YangVideoEncInfo *penc,x265_param *param);
	void init(YangVideoInfo *pvp,YangVideoEncInfo *penc);
	void setVideoMetaData(YangVideoMeta *pvmd);
	void parseRtmpHeader(uint8_t *p, int32_t pLen, int32_t *pwid, int32_t *phei, int32_t *pfps);
	void sendKeyFrame();
private:
	int32_t m_inWidth;
	int32_t m_inHeight;
	int32_t m_outWidth;
	int32_t m_outHeight;
	int32_t m_sendKeyframe;
	//int32_t encode(uint8_t *src,int32_t buflen, uint8_t *dest, int32_t *destLen, int32_t *frametype);
	int32_t encode(YangFrame* pframe, YangEncoderCallback* pcallback);
protected:
	int32_t hex2bin(char *str, char **hex);
	uint8_t *m_nal;
	int32_t nal_len, destLength;
	x265_nal *m_265Nal;
	uint32_t  m_i265Nal;
	x265_picture *m_265Pic;
	x265_encoder *m_265Handle;
	YangLoadLib m_lib;

	void saveFile(char *fileName, uint8_t *pBuffer, int32_t BufferLen);

private:
	//void encode(int32_t buflen, uint8_t *dest, int32_t *destLen, int32_t *frametype);
	char *m_buffer;
	int32_t m_frametype;
	void loadLib();
    	void unloadLib();
    	x265_param *(*yang_x265_param_alloc)(void);
    	void (*yang_x265_param_free)(x265_param *);
    	void    (*yang_x265_param_default)( x265_param * );
    	int32_t (*yang_x265_param_default_preset)(x265_param *, const char *preset, const char *tune);
    	int32_t     (*yang_x265_param_apply_profile)( x265_param *, const char *profile );
    	//int32_t (*yang_x265_picture_alloc)( x265_picture *pic, int32_t i_csp, int32_t i_width, int32_t i_height );
    	x265_picture* (*yang_x265_picture_alloc)();
    	void (*yang_x265_picture_init)(x265_param *param, x265_picture *pic);
    	int32_t     (*yang_x265_encoder_encode)( x265_encoder *, x265_nal **pp_nal,uint32_t  *pi_nal, x265_picture *pic_in, x265_picture *pic_out );
    	void (*yang_x265_picture_free)( x265_picture *pic );
    	//x265_picture_free
    	void    (*yang_x265_encoder_close)( x265_encoder * );
    	x265_encoder* (*yang_x265_encoder_open)( x265_param * );
};

#endif /* YANGENCODER_SRC_YANGH265ENCODERSOFT_H_ */
